package com.fyf.game.net;

import java.util.HashMap;
import java.util.Map;

import org.je.util.StringUtil;

/**
 * @author fanyifeng
 */
public class SenderBox
{
	private Map<String, Sender>	senders;

	public SenderBox() {
		this.senders = new HashMap<String, Sender>();
	}

	/**
	 * @param id
	 */
	public void remove(String id) {
		if (StringUtil.isBlankOrNull(id))
			return;
		synchronized(this) {
			senders.remove(id);
		}
	}

	public void addSender(Sender sender) {
		if (StringUtil.isBlankOrNull(sender.getID()))
			return;
		synchronized(this) {
			senders.put(sender.getID(), sender);
		}
	}

	/**
	 * @param id
	 * @return sender
	 */
	public Sender getSender(String id) {
		if (StringUtil.isBlankOrNull(id))
			return null;
		synchronized(this) {
			return senders.get(id);
		}
	}

	public Sender[] getAllSenders() {
		synchronized(this) {
			if (senders.isEmpty()) {
				return new Sender[0];
			}
			Sender[] arr = new Sender[senders.size()];
			int i = 0;
			for (Sender s : senders.values()) {
				arr[i] = s;
				++i;
			}
			return arr;
		}
	}

	public int size() {
		return senders.size();
	}
}
